//路由
const express = require('express');
const path = require('path');
const moment = require('moment');
const mysql = require('mysql');
const fs = require('fs');
const multer = require('multer');

const router = express.Router(); //app

const uploads = multer({
    dest: path.join(__dirname, '../uplpads')
})




// 设置上传的目录
var upload = multer({
    dest: 'uploads/'
})
//连接数据库
let dbConfig = require('../config/dbconfig.js')
let connection = mysql.createConnection(dbConfig);

//连接mysql
connection.connect(function (err) {
    if (err) {
        console.error('连接失败');
        return;
    }
    console.log('连接mysql成功');
});

//导入控制器
let user = require('../controller/user.js')
const ListController = require("../controller/ListController")
const Controller_content = require('../controller/Controller_content.js');

const test = require('../controller/test.js')

// 1 首页
router.get('/', (req, res) => {
    let userInfo = JSON.parse(req.session.userInfo || '{}');
    res.render('index.html', {
        userInfo
    })
})

// 2 文章表
router.get('/Articlepage', Controller_content.index)

//  3文章回收
router.get('/recyclelist', Controller_content.recyclelist)

//  4 文章删除
router.get('/delete', Controller_content.delete)

// 5 文章添加
router.get('/add', Controller_content.add)

// 实现数据添加入库 
router.post('/insert', upload.single('img'), Controller_content.insert)

// 实现编辑文章页面的回显操作
router.get('/edit', Controller_content.edit)
// 实现文章更新入库操作
router.post('/update', upload.single('img'), Controller_content.update)

//实现文章加入回收站
router.get('/recycle', Controller_content.recycle)

//实现文章还原
router.get('/restore', Controller_content.restore)

//展示一个上传文件的表单
router.get("/addImg", test.addImg)

//处理文件上传
router.post('/upload', upload.single('photo'), Controller_content.upload)

//头像上传
router.post("/uploadAvatar", upload.single('avatar'), user.uploadAvatar)

//用户登录login
router.get('/login', user.login)

//限制用户登录逻辑
router.post('/dologin', user.dologin)

//处理用户退出逻辑
router.get('/logout', user.logout)

//文章详情
router.get('/detail', Controller_content.detail)

//ajaxlogin登录
router.post('/ajaxlogin', user.ajaxlogin)

//注册入库
router.post('/ajaxregister', user.ajaxregister)

//检验邮箱
router.post('/ajaxemail', user.ajaxemail)

router.get("/register", user.register);

//无刷新删除
router.post('/ajaxdelete', Controller_content.ajaxdelete)

//更新密码
router.post('/updatePassword', user.updatePassword)

//分类列表
router.get('/ArticleClassification', ListController.list)

//获取分类表数据的api接口
router.get('/getCateData', ListController.cate)

//删除分类
router.post('/deleteCate', ListController.delete)

//添加分类
router.get('/article', ListController.article)

// 添加分类入库的api接口
router.post('/axAddCate', upload.single(''), ListController.axAddCate)


//展示分类的页面
router.get('/editCate', ListController.editCate)



//更新分类
router.post('/updateCate', ListController.updateCate)


// 给文章添加内容展示视图
router.get('/editContent', Controller_content.editContent)


//编辑文章
router.post('/updateArtilceContent', Controller_content.updateArtilceContent)


// 分类
router.get('/paging',Controller_content.paging)
        

router.get('/ClassificationDataSheet',Controller_content.ClassificationDataSheet)

module.exports = router